<template>
  <!-- 注册 -->
  <div class>
    <div class="login-login">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="usersname" class="iconfont icon-people">
          <el-input type="text" v-model="ruleForm.usersname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="email"
          class="iconfont icon-youxiang"
          :rules="[
                { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ]"
        >
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item prop="pass" class="iconfont icon-ren">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <!-- 记住我 -->
        <el-checkbox v-model="checked1">记住我</el-checkbox>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="iconfont icon-iconfontjiantou"
            :loading="Risloding"
          ></el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  name: "",
  data() {
    /* 用户名验证 */
    var validateusersname = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (!/^[0-9a-zA-Z]{4,16}$/.test(value)) {
        callback(new Error("用户名格式不正确"));
      } else {
        callback();
      }
    };
    /* 密码验证 */
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!/^[0-9a-zA-Z]{6,16}$/.test(value)) {
        callback(new Error("密码格式不正确"));
      }
      callback();
    };
    return {
      ruleForm: {
        pass: "",
        usersname: "",
        email: ""
      },
      rules: {
        usersname: [{ validator: validateusersname, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }]
      },
      /* 是否接受协议 */
      checked1: false,
      Risloding: false
    };
  },
  components: {},
  methods: {
    /* 提交事件 */
    submitForm(formName) {
      this.Risloding = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          /* 发送请求 */
          this.$axios
            .get("/api/regsiter.php", {
              params: this.ruleForm
            })
            .then(res => {
              this.Risloding = false;
              // 成功之后
              if (res.data.code === 3000) {
                this.$message({
                  message: res.data.msg,
                  type: "success"
                });
                setTimeout(() => {
                  this.$router.push("/login/loginCont");
                }, 1000);
              } else if (res.data.code === 3001) {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "error"
                });
              } else if (res.data.code === 3002) {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              } else if (res.data.code === 3003) {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(err => {
              this.isloding = false;
              console.log("err", err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="stylus" scoped>
.login-login {
  position: relative;
  width: 500px;
  margin: -120px auto 0;
  background: #fff;
  box-shadow: border-box;
  padding: 35px 55px 10px 0;
  border-radius: 5px;
}

.el-form-item__content > button {
  position: absolute;
  width: 50px;
  height: 50px;
  margin-top: -148px;
  right: -76px;
  padding: 0;
  border-radius: 50%;
  background-color: #f44336;
  border: 1px solid #f44336;
}

.el-checkbox {
  margin-left: 100px;
}

.el-form {
  position: relative;
}

.icon-youxiang:before {
  position: absolute;
  top: 70px;
  left: 72px;
}

.icon-people:before {
  position: absolute;
  top: 6px;
  left: 74px;
}

.icon-ren:before {
  position: absolute;
  top: 118px;
  font-size: 28px;
  left: 68px;
}

.icon-iconfontjiantou:before {
  font-size: 20px;
}
</style>
